<?php

namespace App\Http\Controllers\Xcx;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Exceptions\CreateException;
use App\Exceptions\DeleteException;
use App\Exceptions\UpdateException;
use App\Xm_history;
use App\Xm_register;
use App\Gc_history;
use App\Needwork;
use App\Craftsman;
use App\Area;
use App\Notice;
use App\Team;
use App\News;

class XmgzController extends Controller
{
    //
    public function store(Request $request)
    {
        $preg = '/^1[3-9]{1}[0-9]{9}$/';
        $pregs = '/^[0-9]+$/';
        $xm = Xm_register::where('uid',auth('xcx')->user()->id)->first();
        if($xm->shenhe == '4'){         //只有项目审核通过的才能进入
            if(auth('xcx')->user()->type == 2){   //只有项目才能招工
                $data = $request->all();

                if(preg_match($preg,$data['phone']) !=1 ) {
                    return $this->failed(208,'请输入正确的手机号');
                }
                if(preg_match($pregs,$data['money']) !=1 ) {
                    return $this->failed(209,'薪资格式不正确');
                }
                $needwork = new Needwork;
                $needwork -> name = $data['name'];
                $needwork -> phone = $data['phone'];
        		// $needwork -> province = $data['province'];
          //       $needwork -> city = $data['city'];
                $needwork -> addr = $data['addr'];
        		$needwork -> times = $data['time'];
        		$needwork -> money = $data['money'];
        		$needwork -> sum = $data['sum'];
        		$work = array_unique($data['work']);//去除重复的工种
        		$needwork -> work = $work;
        		$needwork -> desc = $data['desc'];
                $needwork -> longitude = $data['longitude'];
                $needwork -> latitude = $data['latitude'];
                $needwork -> shenhe = '4';//默认审核为1代表尚未通过
                $needwork -> uid = auth('xcx')->user()->id;
        		$needwork -> save();
        			return $this->success();
        		}else{
        			return $this->failed(201,'提交失败');
        		}
            }else{
                return $this->failed(209,'项目审核尚未通过');
            }
    	
    }
    public function show(Request $request)
    {
        $page = $request -> input('page',1);
        $size = $request -> input('size',10);
        $n = Notice::select('id','title','content','times','zuoze')->limit($size)->offset(($page-1)*$size)->get();
        return $this->success($n);
    }
    public function select(Request $request)
    {
        $page = $request->input('page',1);
        $size = $request->input('size',10);
        $data = Needwork::orderby('updated_at','desc')->where('uid',auth('xcx')->user()->id)->get();
        $arr = [];
        foreach ($data as $k=>$v) {
            $s = Team::find($v->work[0]);
            $arr[$k]['title'] = $v->addr;
            if($s != null) {
                $arr[$k]['title'] = $v->addr.'招'.$s->name;
            }
            $arr[$k]['money'] = $v->money;
            $arr[$k]['men'] = $v->sum;
            $arr[$k]['time'] = $v->times;
            $arr[$k]['address'] = $v->addr;
            $arr[$k]['id'] = $v->id;
        }
        return $this->success($arr);
    }
    public function news()
    {
        $data = News::get();
        return $this->success($data);
    }
    
}
